package com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerAddressDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerInfoDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.impl.PartnerInfoTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditPartnerAddressEntryView extends EditPartnerGenericEntryView<PartnerAddressDTO> {

	private PartnerAddressEntryView partnerAddressEntryView = (PartnerAddressEntryView) getPartnerGenericEntryView();
	private TitleCommandBar titleBar = new TitleCommandBar("New PartnerAddress");
	private TextBox streetTextBox = new TextBox();
	private TextBox numberTextBox = new TextBox();
	private TextBox cityTextBox = new TextBox();
	private TextBox postalCodeTextBox = new TextBox();
	private TextBox townshipTextBox = new TextBox();
	private TextBox stateTextBox = new TextBox();
	private TextBox countryTextBox = new TextBox();
	
	private class SaveClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			getPartnerGenericDTO().setStreet(streetTextBox.getText());
			getPartnerGenericDTO().setNumber(numberTextBox.getText());
			getPartnerGenericDTO().setCity(cityTextBox.getText());
			getPartnerGenericDTO().setPostalCode(postalCodeTextBox.getText());
			getPartnerGenericDTO().setTownship(townshipTextBox.getText());
			getPartnerGenericDTO().setState(stateTextBox.getText());
			getPartnerGenericDTO().setCountry(countryTextBox.getText());
			getPartnerInfoTabView().onPartnerAddressEntrySaved(partnerAddressEntryView, EditPartnerAddressEntryView.this, getPartnerGenericDTO());
		}
	}
	
	private class CancelClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(partnerAddressEntryView != null) 
				partnerAddressEntryView.getPartnerInfoTabView().endEditPartnerAddress(EditPartnerAddressEntryView.this, partnerAddressEntryView);
			else 
				removeFromParent();
		}
	}
	
	public EditPartnerAddressEntryView(PartnerInfoTabViewImpl partnerInfoTabView, PartnerAddressDTO partnerAddressDTO) {
		super(partnerInfoTabView, partnerAddressDTO);
		
		VerticalPanel editPartnerAddressPanel = new VerticalPanel();
		initWidget(editPartnerAddressPanel);
		
		titleBar.addCommand("Save", new SaveClickHandler());
		titleBar.addCommand("Cancel", new CancelClickHandler());
		editPartnerAddressPanel.add(titleBar);
		
		editPartnerAddressPanel.add(new Label("Street"));
		editPartnerAddressPanel.add(streetTextBox);
		editPartnerAddressPanel.add(new Label("Number"));
		editPartnerAddressPanel.add(numberTextBox);
		editPartnerAddressPanel.add(new Label("City"));
		editPartnerAddressPanel.add(cityTextBox);
		editPartnerAddressPanel.add(new Label("Postal Code"));
		editPartnerAddressPanel.add(postalCodeTextBox);
		editPartnerAddressPanel.add(new Label("Township"));
		editPartnerAddressPanel.add(townshipTextBox);
		editPartnerAddressPanel.add(new Label("State"));
		editPartnerAddressPanel.add(stateTextBox);
		editPartnerAddressPanel.add(new Label("Country"));
		editPartnerAddressPanel.add(countryTextBox);
		editPartnerAddressPanel.setStyleName("partnerAddressPanel");
	}
	
	public EditPartnerAddressEntryView(PartnerAddressEntryView partnerAddressEntryView) {
		this(partnerAddressEntryView.getPartnerInfoTabView(), partnerAddressEntryView.getPartnerGenericDTO());
		this.partnerAddressEntryView = partnerAddressEntryView;
		
		titleBar.setTitle("Edit PartnerAddress");
		streetTextBox.setText(getPartnerGenericDTO().getStreet());
		numberTextBox.setText(getPartnerGenericDTO().getNumber());
		cityTextBox.setText(getPartnerGenericDTO().getCity());
		postalCodeTextBox.setText(getPartnerGenericDTO().getPostalCode());
		townshipTextBox.setText(getPartnerGenericDTO().getTownship());
		stateTextBox.setText(getPartnerGenericDTO().getState());
		countryTextBox.setText(getPartnerGenericDTO().getCountry());
	}

}
